package com.yzt.zhuju.photoview;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.maning.imagebrowserlibrary.MNImageBrowser;
import com.maning.imagebrowserlibrary.MNImageBrowserActivity;
import com.maning.imagebrowserlibrary.model.ImageBrowserConfig;
import com.yzt.zhuju.R;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.network.entity.DynamicDetailBean;
import com.yzt.zhuju.permission.PermissionsUtils;
import com.yzt.zhuju.utils.PictureUtils;
import com.yzt.p_base.utils.ToastUtil;
import com.yzt.plugin_imageloader.ImageLoadUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/5
 * desc   :
 */
@SuppressLint("SetTextI18n")
public class PhotoViewManager {

    private final Context mActivity;
    private static PhotoViewManager mInstance;
    private OnClickPhotoListener listener;
    private int position;

    private static Context context;

    public PhotoViewManager(Context activity) {
        this.mActivity = activity;
    }

    public static PhotoViewManager getInstance(Context activity) {
        context = activity;
        if (mInstance == null) {
            mInstance = new PhotoViewManager(activity);
        }
        return mInstance;
    }

    public void showPhotoView(View photoView, List<String> list, int index) {
        showPhotoView(photoView, list, index, null, null);
    }

    public void showPhotoView(View photoView, List<String> list, int index, DynamicDetailBean dynamicDetailBean, OnClickPhotoListener listener) {
        this.listener = listener;
        View view = LayoutInflater.from(mActivity).inflate(R.layout.layout_photo_view, null);
        TextView mTitleTv = view.findViewById(R.id.title_tv);
        ImageView ivMore = view.findViewById(R.id.iv_more);
        LinearLayout layoutLike = view.findViewById(R.id.layout_like);
        LinearLayout layoutHeartbeat = view.findViewById(R.id.layout_heartbeat);
        LinearLayout layoutAction = view.findViewById(R.id.layout_action);
        TextView tvSaveAlbum = view.findViewById(R.id.tv_save_album);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        position = index;
        ivMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutAction.setVisibility(layoutAction.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutAction.setVisibility(View.GONE);
            }
        });
        layoutAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutAction.setVisibility(View.GONE);
            }
        });
        tvSaveAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
                PermissionsUtils.getInstance().checkPermission(MNImageBrowserActivity.getCurrentActivity(), permissions, "Save Picture", new PermissionsUtils.OnCallBackListener() {
                    @Override
                    public void open(List<String> permissionsList) {
                        String url = list.get(position);
                        if (url.contains("?")) {
                            url = url.substring(0, list.get(position).indexOf("?"));
                        }
                        ImageLoadUtil.load4Drawable(AppConfig.context, url, new ImageLoadUtil.ImageRequestListener() {
                            @Override
                            public void onResourceReady(@NonNull @NotNull Drawable resource) {
                                Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
                                PictureUtils.saveBitmap(bitmap, new PictureUtils.OnSaveSuccessListener() {
                                    @Override
                                    public void onSaveSuccess() {
                                        tvSaveAlbum.post(new Runnable() {
                                            @Override
                                            public void run() {
                                                ToastUtil.showMessage("成功");
                                            }
                                        });
                                    }
                                });
                            }

                            @Override
                            public void onError() {

                            }
                        });
                        layoutAction.setVisibility(View.GONE);
                    }

                    @Override
                    public void notOpen(List<String> list, String label) {
                        layoutAction.setVisibility(View.GONE);
                    }
                });
            }
        });
        if (this.listener != null) {
            layoutLike.setVisibility(View.VISIBLE);
            layoutHeartbeat.setVisibility(View.VISIBLE);
            ImageView ivLike = view.findViewById(R.id.iv_like);
            TextView tvLike = view.findViewById(R.id.tv_like);
            ImageView ivHeartbeat = view.findViewById(R.id.iv_heartbeat);
            TextView tvHeartbeat = view.findViewById(R.id.tv_heartbeat);
            ivLike.setImageResource(R.mipmap.icon_dynamic_like);
            tvLike.setText(context.getString(R.string.zan));

            layoutLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onLikeClick(ivLike, tvLike);
                }
            });

            layoutHeartbeat.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onHeartbeat(ivHeartbeat, tvHeartbeat);
                }
            });

        } else {
            layoutLike.setVisibility(View.GONE);
            layoutHeartbeat.setVisibility(View.GONE);
        }
        mTitleTv.setText((index + 1) + "/" + list.size());

        MNImageBrowser.with(mActivity)
                //页面切换效果
                .setTransformType(ImageBrowserConfig.TransformType.Transform_RotateDown)
                //指示器效果
                .setIndicatorType(ImageBrowserConfig.IndicatorType.Indicator_Number)
                //设置隐藏指示器
                .setIndicatorHide(false)
                //设置自定义遮盖层，定制自己想要的效果，当设置遮盖层后，原本的指示器会被隐藏
                .setCustomShadeView(view)
                //自定义ProgressView，不设置默认默认没有
                .setCustomProgressViewLayoutID(R.layout.layout_load_progress)
                //当前位置
                .setCurrentPosition(index)
                //图片引擎
                .setImageEngine(new GlideImageEngine())
                //图片集合
                .setImageList((ArrayList<String>) list)
                //方向设置
                .setScreenOrientationType(ImageBrowserConfig.ScreenOrientationType.ScreenOrientation_Portrait)
                //点击监听
                .setOnClickListener((activity, view1, position, url) -> {

                })
                //页面切换监听
                .setOnPageChangeListener(position -> {
                    this.position = position;
                    if (mTitleTv != null) {
                        mTitleTv.setText((position + 1) + "/" + MNImageBrowser.getImageList().size());
                    }
                })
                //全屏模式
                .setFullScreenMode(false)
                //打开动画
                .setActivityOpenAnime(R.anim.mn_browser_enter_anim)
                //关闭动画
                .setActivityExitAnime(R.anim.mn_browser_exit_anim)
                //手势下拉缩小效果
                .setOpenPullDownGestureEffect(false)
                //显示：传入当前View
                .show(photoView);
    }

    public void setOnClickDeletePhotoListener(OnClickPhotoListener listener) {
        this.listener = listener;
    }

    public void clear() {
        mInstance = null;
        listener = null;
    }

    public interface OnClickPhotoListener {

        void deletePositionPhoto(int position);

        void onLikeClick(ImageView iv_like, TextView tv_like);

        void onHeartbeat(ImageView iv_heartbeat, TextView tv_heartbeat);
    }
}
